package com.attendance.www.hr.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.DepartmentBean;
import com.attendance.www.hr.beans.DepartmentPageBean;
import com.attendance.www.hr.dao.mapper.DepartmentExtMapper;
import com.attendance.www.hr.dao.mapper.DepartmentMapper;
import com.attendance.www.hr.dao.mapper.EmployeeCorporationMapper;
import com.attendance.www.hr.dao.mapper.EmployeeDepartmentMapper;
import com.attendance.www.hr.dao.mapper.ShiftPermissionExtMapper;
import com.attendance.www.hr.entitys.Department;
import com.attendance.www.hr.entitys.DepartmentExample;
import com.attendance.www.hr.entitys.EmployeeDepartment;
import com.attendance.www.hr.entitys.EmployeeDepartmentExample;
import com.attendance.www.hr.service.IDepartmentService;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

	@Resource
	private DepartmentMapper departmentDao;
	@Resource
	private DepartmentExtMapper departmentExtDao;
	@Resource
	private EmployeeCorporationMapper employeeCorporationDao;
	@Resource
	private EmployeeDepartmentMapper employeeDepartmentDao;
	@Resource
	private ShiftPermissionExtMapper shiftPermissionExtDao;

	/**
	 * 添加部门
	 */
	@Override
	public boolean saveDepartment(DepartmentBean departmentBean) {
		Department department = new Department();
		department.setDepartmentCode(departmentBean.getDepartmentCode());
		department.setDepartmentName(departmentBean.getDepartmentName());
		department.setDescription(departmentBean.getDescription());
		department.setCorporationId(departmentBean.getCorporationId());
		department.setOrgLevel(departmentBean.getOrgLevel());
		department.setFromDate(new Date());

		department.setParentDepartmentId(departmentBean.getParentDepartmentId());
		int record = departmentDao.insertSelective(department);

		// if(departmentBean.getParentDepartmentId()!=null) {
		// DepartmentExample example=new DepartmentExample();
		// DepartmentExample.Criteria criteria = example.createCriteria();
		// criteria.andDepartmentIdEqualTo(departmentBean.getParentDepartmentId());
		// List<Department> list = DepartmentDao.selectByExample(example);
		// department.setDepartmentPath(list.get(0).getDepartmentPath()+department.getDepartmentId()+"-");
		// }
		// else {
		// department.setDepartmentPath("-"+department.getDepartmentId()+"-");
		// }
		departmentDao.updateByPrimaryKeySelective(department);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取部门列表带分页
	 */
	@Override
	public DepartmentPageBean getDepartmentList(DepartmentBean departmentBean, Integer startNum, Integer pageCount) {
		List<DepartmentBean> departmentBeanList = departmentExtDao.getDepartmentList(departmentBean, startNum,
				pageCount, true);
		Integer pageTotal = departmentExtDao.getDepartmentListPageTotal(departmentBean);
		DepartmentPageBean departmentPageBean = new DepartmentPageBean();
		departmentPageBean.setDepartmentBeanList(departmentBeanList);
		departmentPageBean.setPageTotal(pageTotal);
		return departmentPageBean;
	}

	/**
	 * 获取部门集合
	 */
	@Override
	public List<DepartmentBean> getDepartmentPageList(DepartmentBean departmentBean, Integer employeeId) {
		// PageName 判断是否为班次设置调用，在根据登录人员ID，获取班次权限管理部门集合
		if ("schedulWeek".equals(departmentBean.getPageName()) && employeeId != null) {
			List<Integer> departmentIdList = shiftPermissionExtDao.getShiftPermissionDepartmentIdList(employeeId);
			if (departmentIdList != null && departmentIdList.size() > 0) {
				departmentBean.setDepartmentIdList(departmentIdList);
			}
		}
		List<DepartmentBean> departmentBeanList = departmentExtDao.getDepartmentList(departmentBean, 0, 0, false);
		return departmentBeanList;
	}

	/**
	 * 修改部门
	 */
	@Override
	public boolean updateDepartment(DepartmentBean departmentBean) {
		Integer departmentId = departmentBean.getDepartmentId();
		Department department = new Department();
		DepartmentExample examples = new DepartmentExample();
		DepartmentExample.Criteria criterias = examples.createCriteria();
		criterias.andDepartmentIdEqualTo(departmentId);
		Department curDepartment = departmentDao.selectByPrimaryKey(departmentId);
		department.setDepartmentId(departmentId);
		department.setDepartmentCode(departmentBean.getDepartmentCode());
		department.setDepartmentName(departmentBean.getDepartmentName());
		department.setDescription(departmentBean.getDescription());
		department.setCorporationId(departmentBean.getCorporationId());
		department.setOrgLevel(departmentBean.getOrgLevel());
		department.setParentDepartmentId(departmentBean.getParentDepartmentId());
		department.setFromDate(departmentBean.getFromDate());
		department.setThruDate(departmentBean.getThruDate());
		department.setIsDisable(0);
		department.setUpdateTime(new Date());
		department.setCreateTime(curDepartment.getCreateTime());

		// if(departmentBean.getParentDepartmentId()!=null) {
		// DepartmentExample example=new DepartmentExample();
		// DepartmentExample.Criteria criteria = example.createCriteria();
		// criteria.andDepartmentIdEqualTo(departmentBean.getParentDepartmentId());
		// List<Department> list = DepartmentDao.selectByExample(example);
		// department.setDepartmentPath(list.get(0).getDepartmentPath()+department.getDepartmentId()+"-");
		// departmentBean.setFromDepartmentPath(curDepartment.getDepartmentPath());
		// departmentBean.setToDepartmentPath(list.get(0).getDepartmentPath()+department.getDepartmentId()+"-");
		// DepartmentExtDao.updateDepartmentPath(departmentBean);
		// } else {
		// department.setDepartmentPath("-"+department.getDepartmentId()+"-");
		// departmentBean.setFromDepartmentPath(curDepartment.getDepartmentPath());
		// departmentBean.setToDepartmentPath("-"+department.getDepartmentId()+"-");
		// DepartmentExtDao.updateDepartmentPath(departmentBean);
		// }
		int record = departmentDao.updateByPrimaryKey(department);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除一条部门数据
	 */
	@Override
	public boolean deleteDepartmentList(Integer departmentId) {
		Department department = new Department();
		department.setDepartmentId(departmentId);
		department.setIsDisable(1);
		department.setDisableTime(new Date());
		department.setUpdateTime(new Date());
		int record = departmentDao.updateByPrimaryKeySelective(department);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据DepartmentId获取一条数据
	 */
	@Override
	public Department getDepartmentByDepartmentId(Integer departmentId) {
		return departmentDao.selectByPrimaryKey(departmentId);
	}

	/**
	 * 根据Department签到时间判断数据库里是否有相同的数据
	 */
	@Override
	public boolean getDepartmentByDepartment(DepartmentBean departmentBean) {
		DepartmentExample example = new DepartmentExample();
		DepartmentExample.Criteria criteria = example.createCriteria();
		criteria.andDepartmentCodeEqualTo(departmentBean.getDepartmentCode());
		if (departmentBean.getDepartmentId() != null) {
			criteria.andDepartmentIdNotEqualTo(departmentBean.getDepartmentId());
		}
		criteria.andIsDisableEqualTo(0);
		List<Department> departmentList = departmentDao.selectByExample(example);
		if (departmentList.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据id获取部门
	 */
	@Override
	public List<EmployeeDepartment> getEmployeeDepartmentListByDepartmentId(Integer departmentId) {
		EmployeeDepartmentExample example = new EmployeeDepartmentExample();
		example.createCriteria().andDepartmentIdEqualTo(departmentId);
		List<EmployeeDepartment> list = employeeDepartmentDao.selectByExample(example);
		return list;
	}

	/**
	 * 根据公司获取部门
	 */
	@Override
	public List<Department> getDepartmentByCorporationId(Integer corporationId) {
		DepartmentExample example = new DepartmentExample();
		example.createCriteria().andCorporationIdEqualTo(corporationId);
		List<Department> list = departmentDao.selectByExample(example);
		return list;
	}

	/**
	 * 根据部门路径获取所属部门
	 */
	@Override
	public List<Department> getDepartmentRoidByPath(Integer departmentId) {
		DepartmentExample example = new DepartmentExample();
		example.createCriteria().andDepartmentPathLike("-" + departmentId + "-");
		List<Department> list = departmentDao.selectByExample(example);
		return list;
	}
}
